<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="商品名称">
                <a-input allow-clear placeholder="请输入商品名称" v-model="queryParam.name"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="商品分类">
                <a-cascader
                  allowClear
                  :options="catList"
                  @change="onCascaderChange"
                  :fieldNames="{ label: 'name', value: 'id', children: 'children' }"
                  placeholder="请选择商品分类"
                />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item>
                <a-button type="primary" @click="getList(true)">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList(true)">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="1"> 已上架 </a-radio-button>
          <a-radio-button value="0"> 已下架 </a-radio-button>
        </a-radio-group>
        <div>
          <a-button type="primary" @click="$router.push({ path: '/saas/supplierPurchaseGoods/edit' })">+ 新增</a-button>
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item @click="allDelete">批量删除</a-menu-item>
            </a-menu>
            <a-button> 批量操作 </a-button>
          </a-dropdown>
        </div>
      </div>
      <a-alert type="info" show-icon style="margin: 10px 0" v-if="selectedRowKeys.length">
        <span slot="message">
          已选择 <span style="color: red">{{ selectedRowKeys.length }}</span> 项
        </span>
      </a-alert>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          current: queryParam.pageNo,
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :rowSelection="{
          selectedRowKeys: this.selectedRowKeys,
          onChange: (e) => {
            this.selectedRowKeys = e
          },
        }"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <div slot="name" slot-scope="text, record">
          <div class="name">
            <div class="pic">
              <img :src="record.pic_url" alt="" />
            </div>
            <div class="info">
              <span>{{ record.name }}</span>
              <span>{{ getCatText(record.cat) }}</span>
            </div>
          </div>
        </div>
        <div slot="send_type" slot-scope="text, record">
          {{ getSendType(text) }}
        </div>
        <span slot="price" slot-scope="text"> ￥{{ text || 0 }} </span>
        <span slot="original_price" slot-scope="text"> ￥{{ text || 0 }} </span>
        <div slot="status" slot-scope="text, scope">
          <a-switch
            :disabled="parseInt(scope.is_audit) !== 2"
            checked-children="上架"
            un-checked-children="下架"
            :checked="scope.status == 1"
            @click="(e) => changeStatus(e, scope)"
          />
        </div>

        <div slot="is_audit" slot-scope="text, scope">
          <div class="tagBox">
            <span class="tag" :style="text | orderStatus"></span>
            <span>{{ getStatus(text) }} </span>
          </div>
        </div>

        <div slot="action" slot-scope="scope">
          <a-button
            :disabled="isDisabled(scope.is_audit)"
            type="link"
            size="small"
            @click="
              $router.push({
                path: '/saas/supplierPurchaseGoods/edit?id=' + scope.id + '&status=' + scope.is_audit,
              })
            "
            >编辑</a-button
          >
          <a-divider type="vertical" style="margin: 0 1px" />

          <!-- <template v-if="parseInt(scope.is_audit) == 0">
            <a-divider type="vertical" />
            <a-button type="link" @click="auditSubmit(scope.id)">审核</a-button>
          </template> -->

          <a-dropdown>
            <a class="ant-dropdown-link" @click="(e) => e.preventDefault()"> 更多 <a-icon type="down" /> </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-button
                  :disabled="isDisabled(scope.is_audit)"
                  size="small"
                  type="link"
                  @click="changeStatus(2, scope)"
                  >删除</a-button
                >
              </a-menu-item>
              <a-menu-item v-if="parseInt(scope.is_audit) == 0">
                <a-button type="link" size="small" @click="auditSubmit(scope.id)">审核</a-button>
              </a-menu-item>
              <a-menu-item>
                <a-button size="small" type="link" @click="see(scope)">查看代理</a-button>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </div>
      </a-table>
    </a-card>

    <a-modal v-model="popDaili" title="产品代理" :width="800">
      <a-table :pagination="false" :columns="dailiColumns" :data-source="dailiList"></a-table>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { goodsList, changeGoodStatus, auditSubmit } from '@/api/purchase/supplier'
import tabList from '@/views/saas/components/tabList.vue'
import { list as catListApi } from '@/api/purchase/cat'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '商品名称',
    scopedSlots: {
      customRender: 'name',
    },
    width: 300,
  },
  {
    title: '产品类型',
    dataIndex: 'send_type',
    scopedSlots: {
      customRender: 'send_type',
    },
  },
  {
    title: '售价',
    dataIndex: 'price',
    scopedSlots: {
      customRender: 'price',
    },
    sorter: (a, b) => a.price - b.price,
  },
  {
    title: '零售价',
    dataIndex: 'original_price',
    scopedSlots: {
      customRender: 'price',
    },
    sorter: (a, b) => a.original_price - b.original_price,
  },
  {
    title: '库存',
    dataIndex: 'num',
    sorter: (a, b) => a.num - b.num,
  },
  // {
  //   title: '采购次数',
  //   dataIndex: 'sold_num',
  //   sorter: (a, b) => a.sold_num - b.sold_num
  // },
  {
    title: '上下架',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '审核状态',
    dataIndex: 'is_audit',
    scopedSlots: {
      customRender: 'is_audit',
    },
  },
  {
    title: '操作',
    width: 130,
    scopedSlots: {
      customRender: 'action',
    },
    fixed: 'right'
  },
]

const dailiColumns = [
  {
    title: '名称',
    dataIndex: 'name',
    width: 150,
  },
  {
    title: '手机号',
    dataIndex: 'mobile',
    width: 150,
  },
  {
    title: '地址',
    dataIndex: 'address',
  },
]

export default {
  components: {
    tabList,
  },

  computed: {
    getCatText() {
      return (cat) => {
        if (cat && cat.length) {
          return cat.map((el) => el.name).join('/')
        } else {
          return '-'
        }
      }
    },

    getSendType() {
      return (send_type) => {
        let str

        switch (parseInt(send_type)) {
          case 0:
            str = '快递'
            break

          case 1:
            str = '集采'
            break

          case 2:
            str = '自主配送'
            break

          case 3:
            str = '代理配送'
            break
        }

        return str
      }
    },

    isDisabled() {
      return (is_audit) => {
        return parseInt(is_audit) === 1
      }
    },

    getStatus() {
      return (is_audit) => {
        let str

        switch (parseInt(is_audit)) {
          case 0:
            str = '未审核'
            break

          case 1:
            str = '审核中'
            break

          case 2:
            str = '已审核'
            break
        }

        return str
      }
    },
  },

  data() {
    return {
      tabList: [
        {
          key: 'saasSupplierPurchaseList',
          name: '商品列表',
        },
        // {
        //   key: 'saasSupplierPurchaseBand',
        //   name: '绑定商品记录',
        // },
        {
          key: 'saasSupplierPurchasePostage',
          name: '运费规则',
        },
      ],

      // 搜索参数
      queryParam: {
        pageNo: 1,
        limit: 10,
        name: '',
        status: '-1',
        goods_id: '',
        cat_id: '',
      },

      selectedRowKeys: [],
      columns,
      list: [],
      catList: [],

      dailiList: [],
      popDaili: false,
      dailiColumns,
    }
  },

  created() {
    if (this.$route.meta.queryParam) {
      this.queryParam = this.$route.meta.queryParam
    }

    this.getList()
    this.getCatList()
  },

  beforeRouteLeave(to, from, next) {
    from.meta.queryParam = this.queryParam
    next()
  },

  filters: {
    sku(e) {
      return '-'
    },

    orderStatus(val) {
      if (val == 0) {
        return 'background-color:#faad14;'
      }

      if (val == 1) {
        return 'background-color:#faad14;'
      }

      if (val == 2) {
        return 'background-color:#65c817;'
      }
    },
  },

  methods: {
    onCascaderChange(val) {
      this.queryParam.cat_id = val[val.length - 1]
    },
    getCatList() {
      catListApi({ is_show: -1 }).then((res) => {
        this.catList = res.data
      })
    },
    auditSubmit(goods_id) {
      this.$confirm({
        title: '提示?',
        content: '是否提交审核',
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        onOk: () => {
          auditSubmit({
            goods_id,
          }).then((res) => {
            if (res.code === 0) {
              this.$message.success('操作成功！')
              this.getList()
              return
            }

            this.$message.error(res.msg)
          })
        },
        onCancel() {},
      })
    },

    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getList()
    },

    // 获取数据
    async getList(reset) {
      if(reset) {
        this.queryParam = {
          ...this.queryParam,
          pageNo: 1
        }
      }

      const hide = this.$message.loading('加载中...', 1000)
      const res = await goodsList(this.queryParam)
      if (res.code == 0) {
        this.list = res.data.list
        this.totalCount = res.data.count
      }
      hide()
    },

    // 切换商品上下架删除状态
    async changeStatus(e, scope) {
      let status

      switch (e) {
        case true:
          status = 1
          break

        case false:
          status = 0
          break

        default:
          status = e
          break
      }

      const res = await changeGoodStatus({ goods_id: scope.id, status })
      if (res.code == 0) {
        this.getList()
      }
    },

    see(scope) {
      this.dailiList = scope.admin_list || []
      this.popDaili = true
    },
  },
}
</script>
<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    margin-top: 11px;
    display: flex;
    align-items: center;
    margin-right: 15px;
  }
  .ant-btn {
    margin-top: 11px;
  }
}
.tip {
  line-height: normal;
  margin-bottom: 0;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}

.tagBox {
  display: flex;
  align-items: center;
}

.tag {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-right: 4px;
}
</style>
